package com.py.korona.ui.activities.categoriesinroom;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import com.py.korona.db.UriBuilder;
import com.py.korona.db.model.CategoriesInRoomEntity;
import com.py.korona.db.model.CategoryEntity;
import cz.destil.settleup.gui.MultiChoiceItem;

public class CategoriesInRoomWrapper {
	public static String getSelectedCategoriesAsCommaString(Long roomId,
			ContentResolver contentResolver) {
		StringBuilder sb = new StringBuilder();
		boolean hasComma = false;
		List<MultiChoiceItem> categories = CategoriesInRoomWrapper
				.categoriesForRoom(roomId, contentResolver);
		for (MultiChoiceItem category : categories) {
			if (category.isChecked()) {
				sb.append((hasComma ? "," : "") + category.text());
				hasComma = true;
			}
		}
		return sb.toString();
	}

	public static List<MultiChoiceItem> categoriesForRoom(Long roomId,
			ContentResolver contentResolver) {
		List<MultiChoiceItem> categoriesInRoom = new ArrayList<MultiChoiceItem>();
		Uri allCategoriesUri = UriBuilder.objectListUri(
				CategoryEntity.uriAuthority(), CategoryEntity.uriBasePath());
		Cursor categoryCursor = contentResolver.query(allCategoriesUri,
				new String[] { CategoryEntity.FieldNameID,
						CategoryEntity.FieldNameText }, null, null, null);
		if (null != categoryCursor) {
			if (categoryCursor.moveToFirst()) {
				do {
					MultiChoiceItem item = new MultiChoiceItem();
					item.setText(categoryCursor.getString(categoryCursor
							.getColumnIndex(CategoryEntity.FieldNameText)));
					item.setUserInfo(categoryCursor.getLong(categoryCursor
							.getColumnIndex(CategoryEntity.FieldNameID)));
					item.setIsChecked(false);
					categoriesInRoom.add(item);
				} while (categoryCursor.moveToNext());
				/**
				 * CategoriesInRoom query
				 * 
				 * @param roomId
				 */
				Uri categoryInRoomUri = null;
				ContentValues contentValues = new ContentValues();
				contentValues.put(CategoriesInRoomEntity.FieldNameRoom, roomId);
				categoryInRoomUri = UriBuilder.objectFilterUri(
						CategoriesInRoomEntity.uriAuthority(),
						CategoriesInRoomEntity.uriBasePath(), contentValues);
				Cursor categoryInRoomCursor = contentResolver
						.query(categoryInRoomUri,
								new String[] { CategoriesInRoomEntity.FieldNameCategory },
								null, null, null);
				if (null != categoryInRoomCursor) {
					if (categoryInRoomCursor.moveToFirst()) {
						do {
							/**
							 * search for category
							 */
							for (MultiChoiceItem multiChoiceItem : categoriesInRoom) {
								if (multiChoiceItem.userInfo().equals(
										categoryInRoomCursor.getLong(0))) {
									multiChoiceItem.setIsChecked(true);
									break;
								}
							}
						} while (categoryInRoomCursor.moveToNext());
					}
					categoryInRoomCursor.close();
				}
			}
			categoryCursor.close();
		}
		return categoriesInRoom;
	}

	public static void saveCategorySelection(Long roomId,
			ContentResolver contentResolver, MultiChoiceItem selection) {
		if (null == selection) {
			return;
		}
		if (null == contentResolver) {
			return;
		}
		long categoryId = (Long) selection.userInfo();
		boolean value = selection.isChecked();
		/*
         * 
         */
		ContentValues cv = new ContentValues();
		cv.put(CategoriesInRoomEntity.FieldNameRoom, roomId);
		cv.put(CategoriesInRoomEntity.FieldNameCategory, categoryId);
		/*
		 * uri for categories in room
		 */
		Uri contentUri = UriBuilder.objectListUri(
				CategoriesInRoomEntity.uriAuthority(),
				CategoriesInRoomEntity.uriBasePath());
		if (value) {
			contentResolver.insert(contentUri, cv);
		} else {
			String whereFields = String.format("%s=? and %s=?",
					CategoriesInRoomEntity.FieldNameRoom,
					CategoriesInRoomEntity.FieldNameCategory);
			String[] whereFieldValues = new String[] { String.valueOf(roomId),
					String.valueOf(categoryId) };
			contentResolver.delete(contentUri, whereFields, whereFieldValues);
		}
	}
}
